#include "ProductTestUartDataProcess.h"

#define DEF_TAG             "TutkSerHandle"

typedef struct
{
    int len;
    EUartCommandType cmd;
}SCommandAndLen,*pSCommandAndLen;

Com_u8 ProductTest_Uart_CheckSum(Com_pu8 recv_buf,Com_u32 len)
{
    Com_s32 j;
    Com_u32 datasum = 0;
    Com_u8 checksum;

    /* check code */
    for (j = 0;j < len; j++)
    {
        datasum += recv_buf[j];
    }

    checksum = datasum % 256;

    return checksum;
}

void ProductTest_UartSend_Data(Com_u8 cmd,Com_pu8 data,Com_s32 len)
{
    unsigned char checksum = 0;
    Com_s32 s_len = 0;
    StProductTestUartData s_data;

    memset(s_data.data,0,sizeof(s_data.data));

    s_data.data[0] = 0x55;
    s_data.data[1] = 0xaa;
    s_data.data[2] = 0x00;
    s_data.data[3] = cmd;
    s_data.data[4] = len / 0x100;
    s_data.data[5] = len % 0x100;

    if(len != 0)
    {
        memcpy(s_data.data + DEF_HEAD_LENGTH,data,len);
    }

    s_len = DEF_HEAD_LENGTH + len;
    checksum = ProductTest_Uart_CheckSum(s_data.data,s_len);
    s_data.data[s_len] = checksum;
    s_data.length = s_len + 1;

    Ext_InPerUartInit_Write(s_data.data,s_data.length);
}

void Ext_ProductTest_UartHandle_Manul_Feed(void)
{
    Com_s32 len = 1;
    Com_u8 data[4];

    memset(data,0,sizeof(data));
    data[0] = 0x01;
    ProductTest_UartSend_Data(COMMAND_MANUL_FEED,data,len);
}

void Ext_ProductTest_UartHandle_Check_Version(void)
{
    Com_s32 len = 0;
    Com_u8 data[4];

    memset(data,0,sizeof(data));
    ProductTest_UartSend_Data(COMMAND_CHECK_BLE_VERSION,data,len);
}

void Ext_ProductTest_UartHandle_Set_Warning_Led(int is_on)
{
    Com_s32 len = 1;
    Com_u8 data[4];

    memset(data,0,sizeof(data));
    data[0] = is_on;
    ProductTest_UartSend_Data(COMMAND_OPEN_MCU_TEST_LED,data,len);
}

void Ext_ProductTest_UartHandle_Reset_MCU(void)
{
    Com_s32 len = 0;
    Com_u8 data[4];

    memset(data,0,sizeof(data));
    ProductTest_UartSend_Data(COMMAND_DEVIVE_RESET,data,len);
}

Com_void Ext_ProductTest_UartHandle_Set_Ble_Mac(Com_void)
{
}

Com_void Ext_ProductTest_UartHandle_Get_Ble_Mac(Com_void)
{
    Com_s32 len = 0;
    Com_u8 data[4];

    memset(data,0,sizeof(data));
    ProductTest_UartSend_Data(COMMAND_GET_BLE_MAC,data,len);
}
